package com.dili.dd.gpi.service.impl;

import com.dili.dd.gpi.domain.Event;
import com.dili.dd.gpi.domain.EventExample;
import com.dili.dd.gpi.exception.BusinessException;
import com.dili.dd.gpi.mapper.EventMapper;
import com.dili.dd.gpi.mapper.ExtEventMapper;
import com.dili.dd.gpi.service.DmaEventManageService;
import com.dili.dd.gpi.util.Pagination;
import com.dili.dd.gpi.util.spring.DataSourceLookupKey;
import com.dili.dd.gpi.util.spring.RequireDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 自定义事件
 * 
 * @author liangzhu
 * 
 */
@Service
public class DmaEventManageServiceImpl implements DmaEventManageService{
    protected static Logger logger = LoggerFactory.getLogger(DmaEventManageServiceImpl.class);

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Resource
    private EventMapper eventMapper;

    @Resource
    private ExtEventMapper extEventMapper;

    @Override
    @RequireDataSource(key = DataSourceLookupKey.DMA_DATASOURCE)
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public List<Event> queryEventByAppkey(String appkey, Pagination pagination) {
        Map<String,Object> params = new HashMap<>();
        params.put("appkey",appkey);
        if(pagination != null){
            params.put("pagination",pagination);
        }
        return extEventMapper.retrieveEventWithPage(params);
    }

    @Override
    @RequireDataSource(key = DataSourceLookupKey.DMA_DATASOURCE)
    @Transactional(readOnly = false,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateEvent(Event event) {
        EventExample eventExample = new EventExample();
        eventExample.createCriteria().andAppkeyEqualTo(event.getAppkey()).andEventIdEqualTo(event.getEventId());
        eventMapper.updateByExampleSelective(event,eventExample);
    }

    @Override
    @RequireDataSource(key = DataSourceLookupKey.DMA_DATASOURCE)
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public Event queryEvent(String appkey, String eventId) throws BusinessException {
        EventExample eventExample = new EventExample();
        EventExample.Criteria criteria = eventExample.createCriteria();
        criteria.andAppkeyEqualTo(appkey)
                .andEventIdEqualTo(eventId);
        List<Event> events = eventMapper.selectByExample(eventExample);
        if(events.size() > 1)
            throw new BusinessException(MessageFormat.format("appkey={0},eventId={1}的事件存在多个。",appkey,eventId));

        return events.get(0);
    }

    @Override
    @RequireDataSource(key = DataSourceLookupKey.DMA_DATASOURCE)
    @Transactional(readOnly = false,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void addEvent(Event event) {
        eventMapper.insert(event);
    }

    @Override
    @RequireDataSource(key = DataSourceLookupKey.DMA_DATASOURCE)
    @Transactional(readOnly = false,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteEvent(String appkey, String itemId) {
        EventExample eventExample = new EventExample();
        eventExample.createCriteria().andAppkeyEqualTo(appkey).andEventIdEqualTo(itemId);
        eventMapper.deleteByExample(eventExample);
    }

    @Override
    @RequireDataSource(key = DataSourceLookupKey.DMA_DATASOURCE)
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public int queryTotalNumByAppkey(String appkey) {
        EventExample eventExample = new EventExample();
        eventExample.createCriteria().andAppkeyEqualTo(appkey);
        return eventMapper.countByExample(eventExample);
    }
}
